package servidor.run.paneles;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import dominio.beans.Proveedor;

import servicio.ServidorImp;

public class ModificarProveedor extends JPanel {

	/**
	 * Create the panel.
	 * @param servidor 
	 */
	public ModificarProveedor(final ServidorImp servidor) {
		JLabel lblRazonSocial = new JLabel("Razon Social");
		
		JLabel lblCuil = new JLabel("Cuil");
		final JTextField txtRazonSoc;
		final JTextField txtCuil;
		txtRazonSoc = new JTextField();
		txtRazonSoc.setColumns(10);
		
		txtCuil = new JTextField();
		txtCuil.setColumns(10);
		
		JButton btnModificarProveedor = new JButton("Modificar Proveedor");
		
		JButton btnBuscarProveedor = new JButton("Buscar Proveedor");
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblCuil, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtCuil))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblRazonSocial)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtRazonSoc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(btnBuscarProveedor)
						.addComponent(btnModificarProveedor))
					.addContainerGap(173, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(23)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblRazonSocial)
						.addComponent(txtRazonSoc, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnBuscarProveedor))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCuil)
						.addComponent(txtCuil, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnModificarProveedor))
					.addContainerGap(220, Short.MAX_VALUE))
		);
		setLayout(groupLayout);
		/**
		 * Eventos
		 */
		btnBuscarProveedor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Proveedor p = servidor.buscarProveedor(txtCuil.getText(), txtRazonSoc.getText());
				txtCuil.repaint();
				txtCuil.validate();
				txtRazonSoc.repaint();
				txtRazonSoc.validate();
	//			System.out.println(p.getNroCuiL() + p.getRazonSocial());
				txtCuil.setText(p.getNroCuiL());
				txtRazonSoc.setText(p.getRazonSocial());
				
			}
		});
		btnModificarProveedor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Proveedor p = servidor.buscarProveedor(txtCuil.getText(), txtRazonSoc.getText());
				if(txtCuil.getText().equals(p.getNroCuiL()))
				{
					p.setRazonSocial(txtRazonSoc.getText());
				}else
				{
					p.setNroCuiL(txtCuil.getText());
				}
			}
		});
	}

}
